package com.glodon.paas.document.service.util;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.glodon.paas.consts.DatabaseConst;
import com.glodon.paas.document.dao.domain.FileInfoEntity;
import com.glodon.paas.exception.PaasServerExceptionFactory;

/**
 * 文件排序类
 * 
 * @author a
 */
@Deprecated
public class OrderUtilsFileEntity {

    // 对FileInfoEntity排序工具类
    private static Map<String, Comparator<FileInfoEntity>> fileEntityOrderMap = new HashMap<String, Comparator<FileInfoEntity>>();
    static {
        fileEntityOrderMap.put("fileName", new NameComparator()); // 文件名
        fileEntityOrderMap.put("extension", new ExtensionComparator()); // 扩展名
        fileEntityOrderMap.put("createTime", new CreateTimeComparator());// 创建时间
        fileEntityOrderMap.put("updateTime", new UpdateTimeComparator());// 创建时间
        fileEntityOrderMap.put("size", new SizeComparator()); //
        fileEntityOrderMap.put("fileSize", new SizeComparator()); //
        fileEntityOrderMap.put("fileType", new ExtensionComparator()); //
    };

    /**
     * 不支持的排序，不会排序
     * 
     * @param list
     * @param orderBy
     * @param orderType
     * @return
     */
    public static List<FileInfoEntity> order(List<FileInfoEntity> list, String orderBy, Integer orderType) {
        Comparator<FileInfoEntity> comparator = (Comparator<FileInfoEntity>) fileEntityOrderMap.get(orderBy);
        if (comparator == null) {
            return list;
        }

        Collections.sort(list, comparator);

        if (orderType != null && orderType.intValue() == DatabaseConst.ORDER_BY_DESC_INT) {
            Collections.reverse(list);
        }

        return list;
    }

    /**
     * 判断orderBy是否可以参与排序
     * 
     * @param orderBy
     * @param exceptionIfError
     * @return
     */
    public static boolean checkOrderBy(String orderBy, boolean exceptionIfError) {
        if ("owner".equals(orderBy) || "type".equals(orderBy)) {
            return true;
        }

        if (fileEntityOrderMap.get(orderBy) == null) {
            if (exceptionIfError) {
                throw PaasServerExceptionFactory.validateException("not suppert order on " + orderBy, null);
            }
            return false;
        }
        return false;
    }

    private static class NameComparator implements Comparator<FileInfoEntity> {

        @Override
        public int compare(FileInfoEntity o1, FileInfoEntity o2) {
            int type = o1.getType().compareTo(o2.getType());
            if (type != 0) {
                return type;
            }
            return o1.getName().compareTo(o2.getName());
        }
    }

    private static class ExtensionComparator implements Comparator<FileInfoEntity> {

        @Override
        public int compare(FileInfoEntity o1, FileInfoEntity o2) {
            int type = o1.getType().compareTo(o2.getType());
            if (type != 0) {
                return type;
            }
            return o1.getExtension().compareTo(o2.getExtension());
        }
    }

    private static class CreateTimeComparator implements Comparator<FileInfoEntity> {

        @Override
        public int compare(FileInfoEntity o1, FileInfoEntity o2) {
            int type = o1.getType().compareTo(o2.getType());
            if (type != 0) {
                return type;
            }
            return o1.getCreateTime().compareTo(o2.getCreateTime());
        }
    }

    private static class UpdateTimeComparator implements Comparator<FileInfoEntity> {

        @Override
        public int compare(FileInfoEntity o1, FileInfoEntity o2) {
            int type = o1.getType().compareTo(o2.getType());
            if (type != 0) {
                return type;
            }
            return -o1.getUpdateTime().compareTo(o2.getUpdateTime());
        }
    }

    private static class SizeComparator implements Comparator<FileInfoEntity> {

        @Override
        public int compare(FileInfoEntity o1, FileInfoEntity o2) {
            int type = o1.getType().compareTo(o2.getType());
            if (type != 0) {
                return type;
            }
            return o1.getSize().compareTo(o2.getSize());
        }
    }
}
